/*
 * @Author: cys
 * @Date: 2018-07-23 10:36:04
 * @Last Modified by: cys
 * @Last Modified time: 2018-10-26 16:01:11
 * 下拉视图混合
 */
import { Component, Vue, Watch, Prop } from "vue-property-decorator";
import { find, isArray } from "lodash";

@Component
export default class TableViewMulSelectMix extends Vue {
  @Prop({ default: true }) public filterable!: boolean;
  @Prop({ type: Boolean, default: false }) public hasObj!: boolean;

  public options = [];

  public change(key: string, val: any) {
    // const obj = this.hasObj ? find(this.options, (o: any) => o[key] === val ) : null;
    const multiple = isArray(val);
    let obj = null;
    if (this.hasObj) {
      obj = multiple ?
        val.reduce((acc: any[], cur: any) => {
          const o = find(this.options, (o: any) => o[key] === cur);
          if (o) {
            acc.push(o);
          }
          return acc;
        }, []) :
        find(this.options, (o: any) => o[key] === val);
    }
    this.$emit("change", val, obj);
    this.$emit("input", val);
  }
}
